package com.aopcinemas.visitor.behaviour;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;

import com.aopcinemas.cinema.model.Movie;
import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.model.Campaign;
import com.aopcinemas.visitor.ontology.GetCampaign;

public class CampaignInfoBehavior extends AchieveREResponder {

    private static final long serialVersionUID = 1L;

    public CampaignInfoBehavior(Agent a, MessageTemplate mt) {
        super(a, mt);
    }

    @Override
    protected ACLMessage handleRequest(ACLMessage request) throws NotUnderstoodException, RefuseException {
        System.out.println("Campaign request received");
        try {
            Action action = (Action) myAgent.getContentManager().extractContent(request);
            GetCampaign getCampaign = (GetCampaign) action.getAction();

            VisitorAgent visitorAgent = (VisitorAgent) myAgent;

            ACLMessage reply = request.createReply();
            Campaign campaign = visitorAgent.getCampaign();
            if (checkCampaign(campaign, getCampaign, request.getSender())) {
                getCampaign.setCampaign(visitorAgent.getCampaign());
                getCampaign.setOwner(myAgent.getAID());
                reply.setPerformative(ACLMessage.INFORM);
                myAgent.getContentManager().fillContent(reply, action);
            } else {
                reply.setPerformative(ACLMessage.REFUSE);
            }
            return reply;
        } catch (CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("Something is wrong");
    }

    boolean checkCampaign(Campaign campaign, GetCampaign getCampaign, AID sender) {
        if (campaign == null || getCampaign == null || sender == null) {
            return false;
        }
        if (campaign.isInParticipants(sender)) {
            return false;
        }
        Schedule campaignSchedule = campaign.getSchedule();
        Movie campaignMovie = campaignSchedule.getMovie();
        Movie checkMovie = getCampaign.getMovie();

        if (checkMovie.getName() != null && !campaignMovie.getName().contains(checkMovie.getName()))
            return false;
        if (checkMovie.getGenre() != null && !campaignMovie.getGenre().equals(checkMovie.getGenre()))
            return false;
        if (checkMovie.getAgeConstraint() != null && !campaignMovie.getAgeConstraint().equals(checkMovie.getAgeConstraint()))
            return false;

        if (getCampaign.getStartTime() != null && !campaignSchedule.getDate().after(getCampaign.getStartTime()))
            return false;
        if (getCampaign.getEndTime() != null && !campaignSchedule.getDate().before(getCampaign.getEndTime()))
            return false;

        if (getCampaign.getCampaign() != null && getCampaign.getCampaign().getType() != null && !campaign.getType().equals(getCampaign.getCampaign().getType()))
            return false;

        return true;
    }
}
